<?php


namespace App\Http\Controllers\api;

use App\Log;
use App\News;
use App\User;
use Illuminate\Http\Request;

class MainController extends BaseController
{
	/**
	 * 获取网盘当前使用人
	 * @return array
	 * @author lilou
	 */
	public function get_net_oss()
	{
		$result = User::select('password')
			->where('id', '=', 1)
			->first();
		return $this->successResponse($result['password']);
	}

	/**
	 * 获取网盘当前使用人
	 * @return array
	 * @author lilou
	 */
	public function get_now_user()
	{
		$last_user = Log::select('name', 'end_time')
			->where('id', '=', Log::max('id'))
			->first();
		if ($last_user['end_time'] === null) {
			return $this->successResponse($last_user['name']);
		}
		return $this->successResponse('null');
	}

	/**
	 * 踢掉网盘当前使用人
	 * @return array
	 * @author lilou
	 */
	public function biu_now_user()
	{

		$last_user = Log::select('name', 'end_time')
			->where('id', '=', Log::max('id'))
			->first();
		if ($last_user['end_time'] === null) {
			$result = Log::where('id', '=', Log::max('id'))
				->update(array('end_time' => date('Y-m-d H:i:s', time())));
			return $this->successResponse('success');
		}
		return $this->successResponse('nobody');
	}

	/**
	 * 申请使用网盘
	 * @param Request $request
	 * @return array
	 * @author lilou
	 */
	public function post_now_user(Request $request)
	{
		$requestData = $request->input('data');
		//判断是否有人使用
		$last_user = Log::select('name', 'end_time')
			->where('id', '=', Log::max('id'))
			->first();
		if ($last_user['end_time'] === null) {
			return $this->errorResponse('use');
		}
		//判断账号密码正确性
		$name = $requestData['name'];
		$password = $requestData['password'];
		$result = User::select('id')
			->where('name', '=', $name)
			->where('password', '=', $password)
			->get();
		if ($result->count() == 0) {
			return $this->errorResponse('different');
		}
		//添加网盘申请记录
		$data['name'] = $name;
		$data['start_time'] = date('Y-m-d H:i:s', time());
		$result = Log::create($data);
		if ($result) {
			return $this->successResponse('success');
		}
		return $this->errorResponse('error');
	}

	/**
	 * 管理员权限检查
	 * @param Request $request
	 * @return array
	 */
	public function check_admin(Request $request)
	{
		$requestData = $request->input('data');
		//判断账号密码正确性
		$name = $requestData['name'];
		$password = $requestData['password'];
		$result = User::select('isAdmin')
			->where('name', '=', $name)
			->where('password', '=', $password)
			->get();
		if ($result->count() == 0) {
			return $this->errorResponse('different');
		}
		return $this->successResponse($result[0]['isAdmin']);
	}

	/**
	 * 修改密码
	 * @param Request $request
	 * @return array
	 */
	public function reset_password(Request $request)
	{
		$requestData = $request->input('data');
		//判断账号密码正确性
		$name = $requestData['name'];
		$password = $requestData['password'];
		$new_password = $requestData['new_password'];
		$result = User::select('id')
			->where('name', '=', $name)
			->where('password', '=', $password)
			->get();
		if ($result->count() == 0) {
			return $this->errorResponse('different');
		}
		//修改密码
		$result = User::where('id', '=', $result[0]['id'])
			->update(array('password' => $new_password));
		if ($result) {
			return $this->successResponse('success');
		}
		return $this->errorResponse('error');
	}

	/**
	 * 修改网盘密码
	 * @param Request $request
	 * @return array
	 */
	public function reset_net_oss(Request $request)
	{
		$requestData = $request->input('data');
		//修改密码
		$password = $requestData['password'];
		$result = User::where('id', '=', 1)
			->update(array('password' => $password));
		if ($result) {
			return $this->successResponse('success');
		}
		return $this->errorResponse('error');
	}


	/**
	 * 新闻列表
	 * @param Request $request
	 * @return array
	 * @author lilou
	 */
	public function getNewsList(Request $request)
	{
		$page = $request->input('page', 1);
		$page = $page > 1 ? $page : 1;
		$size = $request->input('size', 5);

		$list = News::select('id', 'title', 'content')
			->orderBy('created_at', 'desc')
			->offset(($page - 1) * $size)
			->limit($size)
			->get();
		if ($list) {
			$data['list'] = $list;
			$data['count'] = News::count();
			return $this->successResponse($data);
		}

		return $this->errorResponse('error');
	}


	/**
	 * 新闻详情
	 * @param Request $request
	 * @param $NewsID
	 * @return array
	 * @author lilou
	 */
	public function getNewsInfo(Request $request, $NewsID)
	{
		$news = News::find($NewsID);
		if (!$news) {
			return $this->errorResponse('error');
		}
		return $this->successResponse($news);
	}
}
